package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.RequiredPermission;
import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.DataOwnEnum;
import com.electromagnetic.industry.software.common.enums.FilePermission;
import com.electromagnetic.industry.software.common.enums.PrjQuerySource;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.pojo.req.CreateFolderDTO;
import com.electromagnetic.industry.software.manage.pojo.req.FileChunkDTO;
import com.electromagnetic.industry.software.manage.pojo.req.FileInfoQueryDTO;
import com.electromagnetic.industry.software.manage.pojo.req.UpdateFileInfoDTO;
import com.electromagnetic.industry.software.manage.service.EdFileInfoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/data/ed/repo/file")
public class RepoEdFileInfoController {

    @Resource
    private EdFileInfoService edFileInfoService;

    @UserOperation(value = "查看工程树", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping("tree")
    public ElectromagneticResult<?> tree() {
        return edFileInfoService.tree(PrjQuerySource.REPO_DB.value);
    }

    @UserOperation(value = "创建文件夹", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping("createFolder")
    public ElectromagneticResult<?> createFolder(@RequestBody CreateFolderDTO createFolderDTO) {
        return edFileInfoService.createFolder(createFolderDTO, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "作废", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.DELETE)
    @RequestMapping("delete")
    public ElectromagneticResult<?> delete(@RequestParam String id) {
        return edFileInfoService.delete(id, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "查询文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.VIEW)
    @RequestMapping("info")
    public ElectromagneticResult<?> info(@RequestBody FileInfoQueryDTO fileInfoQueryDTO) {
        return edFileInfoService.queryEdFileInfo(fileInfoQueryDTO, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "上传文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.UPLOAD)
    @RequestMapping("upload")
    public ElectromagneticResult<?> upload(@RequestParam("parentId") String parentId,
                                           @RequestParam("file") MultipartFile file,
                                           @RequestParam("strategy") Integer strategy) {
        return edFileInfoService.upload(parentId, file, strategy, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "下载文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.DOWNLOAD)
    @RequestMapping("download")
    public ResponseEntity<InputStreamResource> download(@RequestParam String id, HttpServletResponse response) {
        return edFileInfoService.download(id, response, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "更新文件信息", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.EDIT)
    @RequestMapping("updateFileInfo")
    public ElectromagneticResult<?> updateFileInfo(@RequestBody UpdateFileInfoDTO updateFileInfoDTO) {
        return edFileInfoService.updateFileInfo(updateFileInfoDTO, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "移动文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.MOVE)
    @RequestMapping("moveFile")
    public ElectromagneticResult<?> moveFile(@RequestParam("id") String id,
                                             @RequestParam("targetFolderId") String targetFolderId,
                                             @RequestParam("strategy") Integer strategy) {
        return edFileInfoService.moveFile(id, targetFolderId, strategy, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "复制文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.MOVE)
    @RequestMapping("copyFile")
    public ElectromagneticResult<?> copyFile(@RequestParam("id") String id,
                                             @RequestParam("targetFolderId") String targetFolderId,
                                             @RequestParam("strategy") Integer strategy) {
        return edFileInfoService.copyFile(id, targetFolderId, strategy, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "查看文件历史版本信息", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.VIEW)
    @RequestMapping("versionView")
    public ElectromagneticResult<?> versionView(@RequestParam String fileId) {
        return edFileInfoService.versionView(fileId);
    }

    @UserOperation(value = "回退版本", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.EDIT)
    @RequestMapping("versionBack")
    public ElectromagneticResult<?> versionBack(@RequestParam String fileId, @RequestParam int targetVersion) {
        return edFileInfoService.versionBack(fileId, targetVersion);
    }

    @UserOperation(value = "导出数据库", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping("batchExport")
    public ResponseEntity<InputStreamResource> batchExport(@RequestParam String fileIds, HttpServletResponse response) throws IOException {
        return edFileInfoService.batchExport(fileIds, response, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "批量上传数据库", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping(value = "/mergeChunks", method = RequestMethod.GET)
    public ElectromagneticResult<?> mergeChunks(@RequestParam String identifier,
                                                @RequestParam String fileName,
                                                @RequestParam Integer totalChunks) {
        return edFileInfoService.mergeChunks(identifier, fileName, totalChunks, DataOwnEnum.SYS_FILE.code);
    }

    @UserOperation(value = "批量上传数据库", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping(value = "/batchImport", method = RequestMethod.POST)
    public ElectromagneticResult<?> batchImport(FileChunkDTO fileChunkDTO) {
        return edFileInfoService.batchImport(fileChunkDTO, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "批量上传数据库")
    @RequestMapping(value = "/batchImport", method = RequestMethod.GET)
    public ElectromagneticResult<?> checkChunkExist(FileChunkDTO fileChunkDTO) {
        return edFileInfoService.checkChunkExist(fileChunkDTO, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "查询发布管理", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequestMapping(value = "/uploadRecord", method = RequestMethod.GET)
    public ElectromagneticResult<?> uploadRecord(@RequestParam int pageNum, @RequestParam int pageSize) {
        return edFileInfoService.uploadRecord(pageNum, pageSize, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "查询文件详细信息", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.VIEW)
    @RequestMapping(value = "/fileDetail", method = RequestMethod.GET)
    public ElectromagneticResult<?> detail(@RequestParam String id) {
        return edFileInfoService.detail(id);
    }

    @UserOperation(value = "查询子文件集", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.VIEW)
    @RequestMapping(value = "/queryChildFolder", method = RequestMethod.GET)
    public ElectromagneticResult<?> queryChildFolder(@RequestParam String parentId) {
        return edFileInfoService.queryChildFolder(parentId, DataOwnEnum.REPO_FILE.code);
    }

    @UserOperation(value = "预览文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    @RequiredPermission(value = FilePermission.PREVIEW)
    @RequestMapping(value = "preview", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> preview(@RequestParam String id, HttpServletResponse response) {
        return edFileInfoService.preview(id, response, DataOwnEnum.REPO_FILE.code);
    }

    /**
     * 添加进收藏夹
     *
     * @param id 文件id
     * @return
     */
    @GetMapping("/collection")
    @UserOperation(value = "收藏了文件", modelName = UserOperationModuleEnum.REPO_PRJ_DATABASE)
    public ElectromagneticResult<?> addFavorite(@RequestParam String id) {
        String userId = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edFileInfoService.addFavorite(userId, id));
    }

    /**
     * 从收藏夹移除
     *
     * @param id 文件id
     * @return
     */
    @GetMapping("/removeCollection")
    @UserOperation(value = "从收藏夹移除文件", modelName = UserOperationModuleEnum.USER_PRJ)
    public ElectromagneticResult<?> removeFavorite(@RequestParam String id) {
        String userId = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edFileInfoService.removeFavorite(userId, id));
    }
}
